package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetMealDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: yp
 */
@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private JedisPool jedisPool;


    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     */
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //向t_setmeal插入一条记录
        setMealDao.add(setmeal);
        // 向t_setmeal_checkgroup插入多条记录
        Integer setmealId = setmeal.getId();
        setSetmealAndCheckGroup(setmealId,checkgroupIds);
        //把图片存到数据库的set【redis】
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }


    /**
     * 查询所有的套餐
     *
     * @return
     */
    public List<Setmeal> getSetmeal() {
        List<Setmeal> setmealList = null;
        try {
            String allSetMeal = jedisPool.getResource().get("allSetMeal");
            System.out.println("allSetMeal="+allSetMeal);
            if (allSetMeal!=null){
                System.out.println("数据来自redis中....");
                setmealList = JSON.parseArray(allSetMeal, Setmeal.class);
                return setmealList;
            }
            System.out.println("数据来自mysql中....");
            setmealList = setMealDao.getSetmeal();
            jedisPool.getResource().setex("allSetMeal",5*60, JSON.toJSON(setmealList).toString());
            return setmealList;
        } catch (Exception e) {
            System.out.println("redis异常,查询数据库....");
            setmealList = setMealDao.getSetmeal();
            return setmealList;
        }

    }

    /**
     * 套餐详情(1.先直接查询套餐的基本信息 2.关联查询出检查组的信息List 3.关联查询出检查项的信息List)
     * @param id
     * @param type
     * @return
     */
    public Setmeal findById(Integer id, Integer type) {
        Setmeal setmeal = null;
        try {
            String setm = jedisPool.getResource().get("setmeal" + id);
            System.out.println("setm="+setm);
            if (setm!=null){
                System.out.println("套餐详情数据来自redis中....");
                setmeal = JSON.parseObject(setm, Setmeal.class);
                verdict(setmeal,type);
                return setmeal;
            }
            System.out.println("套餐详情数据来自mysql中....");
            setmeal = setMealDao.findById(id);
            jedisPool.getResource().setex("setmeal" + id,5*60,JSON.toJSONString(setmeal));
            verdict(setmeal,type);
            //取出setmeal的id, 调用CheckGroupDao 查询出List<CheckGroup> list 封装
            //遍历List<CheckGroup> list, 取出每一个CheckGroup的id, 调用CheckItemDao 查出 List<CheckItem> checkItems;
            return setmeal;
        } catch (Exception e) {
            System.out.println("redis异常,查询数据库....");
            setmeal = setMealDao.findById(id);
            verdict(setmeal,type);
            return setmeal;
        }
    }

    public List<Map<String, Object>> getSetmealReport() {

        List<Map<String, Object>> setmealReport = setMealDao.getSetmealReport();

        System.out.println("setmealReport:"+setmealReport);

       /* return setMealDao.getSetmealReport();*/

        return setmealReport;
    }

    private void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds) {
        if(checkgroupIds != null){
            for (Integer checkgroupId : checkgroupIds) {
                Map map = new HashMap();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setMealDao.setSetmealAndCheckGroup(map);
            }
        }
    }

    public void verdict(Setmeal setmeal,Integer type){
        if(type == 1) {
            //不懒(使用一下)
            List<CheckGroup> checkGroups = setmeal.getCheckGroups();
            System.out.println(checkGroups.size());
            for (CheckGroup checkGroup : checkGroups) {
                List<CheckItem> checkItems = checkGroup.getCheckItems();
                System.out.println(checkItems);
            }
        }
    }
}
